package com.cash.app.ui.loan

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.cash.app.base.BaseViewModel
import com.cash.app.manager.api.GlobalAPIService
import com.cash.app.manager.model.ProductIdRequest
import com.cash.app.manager.model.VipProductResponse
import com.cash.app.net.NetRequestExecutor
import com.cash.app.net.RetrofitClient
import com.cash.app.ui.loan.api.LoanAPIService
import com.cash.app.ui.loan.model.ApplyOrdersRequest
import com.cash.app.ui.loan.model.ApplyOrdersResponse
import com.cash.app.ui.loan.model.PrepareRequest
import com.cash.app.ui.loan.model.PrepareResponse
import com.cash.app.ui.loan.model.RecommendDataResponse

class LoanConfirmACViewModel : BaseViewModel() {

    private val _prepareData = MutableLiveData<PrepareResponse>()
    val prepareData = _prepareData

    private val _recommendData = MutableLiveData<RecommendDataResponse>()
    val recommendData = _recommendData

    private val _applyOrdersData = MutableLiveData<ApplyOrdersResponse>()
    val applyOrdersData = _applyOrdersData

    private val loanAPIService by lazy {
        RetrofitClient.createService(LoanAPIService::class.java)
    }

    private val globalAPIService by lazy {
        RetrofitClient.createService(GlobalAPIService::class.java)
    }

    private val _vipProduct = MutableLiveData<VipProductResponse>()
    val vipProduct: LiveData<VipProductResponse> = _vipProduct

    fun getPrepareData(pId: Int, pMoney: Int, pPeriod: Int) {
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                loanAPIService.getPrepareData(PrepareRequest(pId, pMoney, pPeriod))
            }
        }, onSuccess = { response ->
            response?.let {
                _prepareData.value = it
            }
        })
    }

    fun getRecommendData(productId: Int) {
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                loanAPIService.getRecommendData(ProductIdRequest(productId))
            }
        }, onSuccess = { response ->
            response?.let {
                _recommendData.value = it
            }
        })
    }

    fun applyOrder(productList: List<ApplyOrdersRequest.Product>) {
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                loanAPIService.applyOrder(ApplyOrdersRequest(productList))
            }
        }, onSuccess = { response ->
            response?.let {
                _applyOrdersData.value = it
            }
        }, onError = { _, _ ->
        })
    }

    fun getVipProduct() {
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                globalAPIService.getVipProduct()
            }
        }, onSuccess = { response ->
            response?.let {
                _vipProduct.value = it
            }
        })
    }
} 